#!/usr/bin/env node

const Router = require('koa-router'),
	dynamic = require('../models/dynamic_mod'),
	image = require('../models/image_mod'),
	sj = require('silly-datetime'),
	router = new Router;

router.prefix('/api/dynamic');
router.post('/', async (ctx, next) => {
	let file = ctx.request.files.File;
	let filename = new Array();
	let time = sj.format(new Date());
	if (file) {
		if (file.length == undefined) {
			filename[0] = await image.Image(file);
		} else if (file.length > 1) {
			filename = await image.Images(file);
		}
	}
	let { userd_id, dynamic_title, dynamic_context } = ctx.request.body;
	let res = await dynamic.Dynamic(userd_id, dynamic_title, dynamic_context, time, filename);
	ctx.body = res;
})
router.post('/dynamic', async (ctx, next) => {
	let { userd_id } = ctx.request.body;
	let res = await dynamic.Dynamic_get_one(userd_id);
	ctx.body = res;
})
router.post('/dynamic2', async (ctx, next) => {
        let { userd_id } = ctx.request.body;
        let res = await dynamic.Dynamic_get_one2(userd_id);
        ctx.body = res;
})
router.delete('/', async (ctx, next) => {
	let { dynamic_id } = ctx.request.body;
	let res = await dynamic.Dynamic_delete(dynamic_id);
	ctx.body = res;
})
router.get('/', async (ctx, next) => {
	let res = await dynamic.Dynamic_Get();
	ctx.body = res;
})
router.get('/dynamic', async (ctx, next) => {
	let res = await dynamic.Dynamic_get();
	ctx.body = res;
})
module.exports = router;
